Skip to content
Navigation Menu
Sign in
Appearance settings
Platform
AI CODE CREATION
GitHub Copilot
Write better code with AI
GitHub Copilot app
Direct agents from issue to merge
MCP Registry
Integrate external tools
DEVELOPER WORKFLOWS
Actions
Automate any workflow
Codespaces
Instant dev environments
Issues
Plan and track work
Code Review
Manage code changes
Code Quality
Enforce quality at merge
APPLICATION SECURITY
GitHub Advanced Security
Find and fix vulnerabilities
Code security
Secure your code as you build
Secret protection
Stop leaks before they start
EXPLORE
Why GitHub
Documentation
Blog
Changelog
Marketplace
View all features
Solutions
BY COMPANY SIZE
Enterprises
Small and medium teams
Startups
Nonprofits
BY USE CASE
App Modernization
DevSecOps
DevOps
CI/CD
View all use cases
BY INDUSTRY
Healthcare
Financial services
Manufacturing
Government
View all industries
View all solutions
Resources
EXPLORE BY TOPIC
AI
Software Development
DevOps
Security
View all topics
EXPLORE BY TYPE
Customer stories
Events & webinars
Ebooks & reports
Business insights
GitHub Skills
SUPPORT & SERVICES
Documentation
Customer support
Community forum
Trust center
Partners
View all resources
Open Source
COMMUNITY
GitHub Sponsors
Fund open source developers
PROGRAMS
Security Lab
Maintainer Community
GitHub Stars
Archive Program
REPOSITORIES
Topics
Trending
Collections
Enterprise
ENTERPRISE SOLUTIONS
Enterprise platform
AI-powered developer platform
AVAILABLE ADD-ONS
GitHub Advanced Security
Enterprise-grade security features
Copilot for Business
Enterprise-grade AI features
Premium Support
Enterprise-grade 24/7 support
Pricing
Search
/
Sign in
Sign up
Appearance settings
You signed in with another tab or window.
Reload
to refresh your session.
You signed out in another tab or window.
Reload
to refresh your session.
You switched accounts on another tab or window.
Reload
to refresh your session.
Dismiss alert
{{ message }}
ascendho
cpp-bootcamp
Repository navigation
Code
Issues
1
(1)
Pull requests
Actions
Projects
Security and quality
Insights
More
items
Files
Expand file tree
master
Breadcrumbs
cpp-bootcamp
/
6 - Synch Primitives
/
condition_variable.cpp
Copy path
Blame
More file actions
Blame
More file actions
Latest commit
History
History
History
66 lines (57 loc) · 2.34 KB
master
Breadcrumbs
cpp-bootcamp
/
6 - Synch Primitives
/
condition_variable.cpp
Copy path
Top
File metadata and controls
Code
Blame
66 lines (57 loc) · 2.34 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
// 这个程序展示了std::condition_variable使用的一个小例子。
// std::condition_variable类提供了条件变量同步原语。
// 条件变量原语允许线程在获取互斥锁之前等待特定条件。
// 它还允许其他线程向等待线程发出信号,提醒它们条件可能为真。
// 有关C风格条件变量的更详细介绍,请参见
// https://pages.cs.wisc.edu/~remzi/OSTEP/threads-cv.pdf。
// 这个程序运行三个线程。
// 其中两个线程运行一个函数,该函数原子地将全局count变量增加1,
// 并在count变量为2时通知等待线程。
// 当count变量为2时,等待线程唤醒,获取锁,并打印count值。
// 包含条件变量库头文件。
#include <condition_variable>
// 包含std::cout(打印)用于演示目的。
#include <iostream>
// 包含mutex库头文件。
#include <mutex>
// 包含thread库头文件。
#include <thread>
// 定义一个全局count变量、一个mutex和一个条件变量供两个线程使用。
int count = 0;
std::mutex m;
// 这是声明和默认初始化条件变量的语法。
std::condition_variable cv;
// 在这个函数中,线程将count变量增加1。
// 如果count值为2,它也会通知一个等待线程。
// 它由main函数中的两个线程运行。
void add_count_and_notify() {
std::scoped_lock slk(m);
count += 1;
if (count == 2) {
cv.notify_one();
}
}
// 这个函数由等待线程运行,等待条件count == 2。
// 之后,它获取互斥锁m并在临界区执行代码。
// 条件变量需要构造一个std::unique_lock对象。
// std::unique_lock是一种C++ STL同步原语类型,
// 提供更多灵活性和功能,包括与条件变量一起使用。
// 特别地,它是可移动的,但不可复制构造或复制赋值。
void waiter_thread() {
std::unique_lock lk(m);
cv.wait(lk, []{return count == 2;});
std::cout << "Printing count: " << count << std::endl;
}
// main方法构造三个thread对象,
// 并让其中两个并行运行add_count_and_notify函数。
// 这些线程执行完毕后,我们从等待线程打印count值,
// 显示两个增量操作以及等待线程中的条件获取都成功工作了。
int main() {
std::thread t1(add_count_and_notify);
std::thread t2(add_count_and_notify);
std::thread t3(waiter_thread);
t1.join();
t2.join();
t3.join();
return 0;
}
You can’t perform that action at this time.